import Angle from './Angle';
import { SpotI, AngleI } from '@/utils/CanvasUtils/CanvasType';

/** 二维点计算类 */
export default class Point implements SpotI {

	/** X位置 */
	private x: number;

	/** Y位置 */
	private y: number;

	/** X位置 */
	public get X(): number {
		return this.x;
	}
	public set X(val: number) {
		this.x = val;
	}

	/** Y位置 */
	public get Y(): number {
		return this.y;
	}
	public set Y(val: number) {
		this.y = val;
	}

	/**
	 * 构造函数
	 * @param x x位置
	 * @param y y位置
	 */
	constructor(x: number, y: number) {
		this.x = x;
		this.y = y;
	}

	/**
	 * 移动
	 * @param X x移动距离
	 * @param Y y移动距离
	 */
	public Move(X: number, Y: number) {
		return new Point(this.X + X, this.Y + Y);
	}

	/**
	 * 旋转
	 * @param Source 参照点
	 * @param Angle 旋转角度
	 */
	public Rotate(Source: SpotI, Angle: AngleI): Point {
		const { X: X1, Y: Y1 } = Source;
		const X = this.X - X1;
		const Y = this.Y - Y1;
		const A = Math.atan2(Y, X) + Angle.Value;
		const R = Math.sqrt(X * X + Y * Y);
		return new Point(Math.cos(A) * R + X1, Math.sin(A) * R + Y1);
	}

	/**
	 * 获取角度
	 * @param Source 参照点
	 * @returns 两点之间的角度
	 */
	public GetAngle(Source: SpotI): Angle {
		const { X: X2, Y: Y2 } = this;
		const { X: X1, Y: Y1 } = Source;
		const ret: Angle = new Angle(0);
		const def_x = X2 - X1;
		const def_y = Y2 - Y1;
		const value = Math.atan(def_x / def_y);
		if (isNaN(value)) {
			return ret;
		}
		ret.Value = value;
		if (def_y >= 0) {
			ret.Angle = 90 - ret.Angle;
		}
		if (def_y < 0) {
			ret.Angle = 270 - ret.Angle;
		}
		return ret;
	}
}
